package vip.xiaonuo.smzq.modular.nbfb.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.excel.CommonExcelCustomMergeStrategy;
import vip.xiaonuo.common.util.CommonResponseUtil;
import vip.xiaonuo.smzq.core.tools.file.PathUtils;
import vip.xiaonuo.smzq.modular.kcl.entity.Zycl;
import vip.xiaonuo.smzq.modular.nbfb.entity.param.NbfbTwoParam;
import vip.xiaonuo.smzq.modular.nbfb.entity.vo.NbfbOneVo;
import vip.xiaonuo.smzq.modular.nbfb.entity.vo.NbfbThreeVo;
import vip.xiaonuo.smzq.modular.nbfb.entity.vo.NbfbTwoVo;
import vip.xiaonuo.smzq.modular.nbfb.mapper.ZyclTwoMapper;
import vip.xiaonuo.smzq.modular.nbfb.service.INbfbTwoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NbfbTwoServiceImpl extends ServiceImpl<ZyclTwoMapper, Zycl> implements INbfbTwoService {
    @Resource
    private ZyclTwoMapper zyclTwoMapper;
    @Value("${file.base}")
    String baseFilePath;





    /**
     * 年报附表2
     * SQQ
     * @return
     */
    @Override
    public List<NbfbTwoVo> getNbfbTwo(NbfbTwoParam param) {
        List<NbfbTwoVo> list = new ArrayList<>();
        try{
            list=  zyclTwoMapper.getNbfbTwo(param.getNd());
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 导出年报附表2
     * SQQ
     * @return
     */
    @Override
    public void exportNbfbTwo(NbfbTwoParam param, HttpServletResponse response) {
        exportNbfbTwoTemplate(param, response);
    }

    public void exportNbfbTwoTemplate(NbfbTwoParam param, HttpServletResponse response) {
        //年报附表1模板路径
        String templateFilePath = PathUtils.getNbfbPath(2);
        Integer nd=param.getNd();
        File tempFile = null;
        // 加载模板文件
        ClassPathResource classPathResource = new ClassPathResource(templateFilePath);
        InputStream inputStream =  classPathResource.getStream();

        try {
            List<NbfbTwoVo> list = getNbfbTwo(param);
            String fileName = nd + "年报报表附表2.xlsx";
            tempFile = new File(PathUtils.getUpLoadPath(baseFilePath) + File.separator + fileName);

            try (ExcelWriter excelWriter = EasyExcel.write(tempFile.getPath())
                    .registerWriteHandler(new CommonExcelCustomMergeStrategy(list.stream().map(NbfbTwoVo::getKl)
                            .collect(Collectors.toList()), 0))
                    .withTemplate(inputStream).build()
            ) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                // 使用 fill 方法填充数据
                excelWriter.fill(list, writeSheet);
            }
            // 将生成的文件发送给客户端
            IoUtil.write(response.getOutputStream(), true, FileUtil.readBytes(tempFile));
        } catch (Exception e) {
            log.error(nd + "年报报表附表2：", e);
            try {
                CommonResponseUtil.renderError(response, nd + "年报报表附表2");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            FileUtil.del(tempFile);
        }




    }






}
